package com.zlp.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @author: zhulaiping
 * @time: 2022/8/29 13:37
 */
public class NettyHelloClient {

    public static void main(String[] args) throws InterruptedException {
        //1.启动类
        Channel channel = new Bootstrap()
                //添加EventLoop
                .group(new NioEventLoopGroup())
                //选择channel的实现，客户端使用socketChannel
                .channel(NioSocketChannel.class)
                //添加处理器
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    //连接后被调用
                    protected void initChannel(Channel channel) throws Exception {
                        channel.pipeline().addLast(new StringEncoder());
                    }
                }).connect(new InetSocketAddress("localhost", 8080))
                .sync()//阻塞方法，知道连接建立
                .channel();//代表连接对象

        System.out.println(channel);
        System.out.println("");

    }
}
